package com.xinruke.hostels.reservation.service.pms;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.cache.extract.extractor.HotelInfoCacheExtractor;
import com.xinruke.hostels.central.manage.interfaces.dto.HotelInfoDetailQueryResultDTO;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.cache.ReservationUserCache;
import com.xinruke.hostels.common.cache.util.SysCodeUtil;
import com.xinruke.hostels.common.constants.BusiConstant;
import com.xinruke.hostels.common.constants.RedisConstants;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.exception.ArgumentException;
import com.xinruke.hostels.common.exception.BizException;
import com.xinruke.hostels.common.po.*;
import com.xinruke.hostels.common.po.constants.HuiyidanjiudianguanxiConstants;
import com.xinruke.hostels.common.po.constants.YwFangtairiqijiluConstants;
import com.xinruke.hostels.common.po.constants.YwHuiyidanjiudianguanxiConstants;
import com.xinruke.hostels.common.po.constants.YwYudingHuiyiDingdanConstants;
import com.xinruke.hostels.common.service.OperateLogService;
import com.xinruke.hostels.common.util.*;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.reservation.interfaces.dto.*;
import com.xinruke.hostels.reservation.interfaces.dto.pms.*;
import com.xinruke.hostels.reservation.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MeetingBookingService {

    @Resource
    private YwYudingHuiyiXuqiudanMapper ywYudingHuiyiXuqiudanMapper;

    @Resource
    private YwYudingHuiyiDingdanMapper ywYudingHuiyiDingdanMapper;

    @Resource
    private YwYudingHuiyiDingdanZixiangMapper ywYudingHuiyiDingdanZixiangMapper;

    @Resource
    private YwHuiyidanjiudianguanxiMapper huiyidanjiudianguanxiMapper;

    @Autowired
    private YwFangtairiqijiluMapper fangtairiqijiluMapper;

    @Resource
    private ReservationUserCache reservationUserCache;

    @Resource
    private HotelInfoCacheExtractor hotelInfoCacheExtractor;

    @Autowired
    private YwHuichangMapper huichangMapper;

    @Autowired
    private OperateLogService operateLogService;

    @Autowired
    private ManageUserCache manageUserCache;

    @Resource(name = "commonRedisTemplate")
    private RedisTemplate commonRedisTemplate;


    @Transactional
    public ResultVO submit(MeetingBookingInfoDto params) {
        log.info("会议预订下单接口 start---------");
        String traceId = CustomSecurity.getTraceID();
        log.info("traceId[" + traceId + "] 请求参数：reqParam=" + JSONObject.toJSONString(params));
        long startTime = System.currentTimeMillis();

        ResultVO resultVO = ResultVO.fail();
        String dingdanhao = "HYDD" + String.valueOf(System.currentTimeMillis()).substring(5);
        int jiudianId = manageUserCache.getJiudianId();
        String redisLockKey = RedisConstants.HOTEL_ROOM_LOCK_KEY_PREFIX + jiudianId;
        try {
            //使用redis，根据酒店id对房间操作加锁
            boolean lockRet = commonRedisTemplate.opsForValue().setIfAbsent(redisLockKey, jiudianId);
            if (!lockRet) {
                log.error("traceId[" + traceId + "]房间冲突，请重试");
                resultVO.setMessage("房间冲突，请重试");
                return resultVO;
            }


            // 1.校验请求
            resultVO = checkRequest(params);
            if (ResultVO.SUCCESS != resultVO.getCode()) {
                return resultVO;
            }
            //2.校验具体业务参数正确性
            //2.1校验会议预订开始-结束时间是否已存在预订
            BigDecimal totalAmount = BigDecimal.ZERO;
            List<MeetingBookingInfoHuichangDetailDto> meetingBookingInfoHuichangDetailList = params.getDetailList();
            for (MeetingBookingInfoHuichangDetailDto meetingBookingInfoHuichangDetailDto : meetingBookingInfoHuichangDetailList) {
                YwFangtairiqijilu fangtairiqijilu = new YwFangtairiqijilu();
                fangtairiqijilu.setFangjianId(meetingBookingInfoHuichangDetailDto.getHuichangId());
                fangtairiqijilu.setFangjianleibie(YwFangtairiqijiluConstants.FANGJIANLEIBIE_HUIYISHI);
                fangtairiqijilu.setKaishishijian(meetingBookingInfoHuichangDetailDto.getKaishishijian());
                fangtairiqijilu.setJieshushijian(meetingBookingInfoHuichangDetailDto.getJieshushijian());
                List<YwFangtairiqijilu> fangtairiqijiluList = fangtairiqijiluMapper.selectBookListByEntity(fangtairiqijilu);
                log.info("traceId[" + traceId + "]会场预订信息 fangtairiqijiluList=" + JSONObject.toJSONString(fangtairiqijiluList));
                if (!CollectionUtils.isEmpty(fangtairiqijiluList)) {
                    log.error("traceId[" + traceId + "]会场[" + meetingBookingInfoHuichangDetailDto.getHuichangbianhao() + "]已占用");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("会场[" + meetingBookingInfoHuichangDetailDto.getHuichangbianhao() + "]已占用");
                    return resultVO;
                }
                totalAmount = totalAmount.add(meetingBookingInfoHuichangDetailDto.getYouhuijia());
            }
            //2.2校验预订单总金额
            if (params.getDingdanzongjia().compareTo(totalAmount) != 0) {
                log.error("traceId[" + traceId + "] 订单总价[{}] 实际金额[{}] 订单总价金额不对", params.getDingdanzongjia(), totalAmount);
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("订单总价金额不对");
                return resultVO;
            }

            //根据需求单ID和酒店ID修改询价单的是否满足需求标识
            Example example = Example.builder(YwHuiyidanjiudianguanxi.class)
                    .where(Sqls.custom()
                            .andEqualTo("jiudianId", manageUserCache.getJiudianId())
                            .andEqualTo("huiyidingdanId", params.getYudingxuqiudanId())
                            .andEqualTo("shanchubiaoshi", SysDicConstant.DELFLAG_0)
                    ).build();
            List<YwHuiyidanjiudianguanxi> ywHuiyidanjiudianguanxis = huiyidanjiudianguanxiMapper.selectByExample(example);
            if (ywHuiyidanjiudianguanxis == null || ywHuiyidanjiudianguanxis.size() == 0 || ywHuiyidanjiudianguanxis.get(0).getZhuangtai().equals(YwHuiyidanjiudianguanxiConstants.ZHUANGTAI_BUTONGGUO)){
                resultVO.setMessage("订单不存在或审核未通过");
                return resultVO;
            }
            YwHuiyidanjiudianguanxi ywHuiyidanjiudianguanxi = new YwHuiyidanjiudianguanxi();
            ywHuiyidanjiudianguanxi.setManzuxuqiu(YwHuiyidanjiudianguanxiConstants.MANZUXUQIU_MANZU);
            int i = huiyidanjiudianguanxiMapper.updateByExampleSelective(ywHuiyidanjiudianguanxi, example);
            if (i<1){
                resultVO.setMessage("修改询价单满足需求标识失败");
                return resultVO;
            }

            //将需求单状态修改成已回复
            YwYudingHuiyiXuqiudan ywYudingHuiyiDingdan = new YwYudingHuiyiXuqiudan();
            ywYudingHuiyiDingdan.setId(params.getYudingxuqiudanId());
            ywYudingHuiyiDingdan.setZhuangtai(YwYudingHuiyiDingdanConstants.ZHUANGTAI_YIHUIFU);
            YwYudingHuiyiXuqiudan ywYudingHuiyiDingdanQueryVo = ywYudingHuiyiXuqiudanMapper.selectOne(ywYudingHuiyiDingdan);
            if (ywYudingHuiyiDingdanQueryVo != null) {
                YwYudingHuiyiXuqiudan ywYudingHuiyiDingdanUpVo = new YwYudingHuiyiXuqiudan();
                ywYudingHuiyiDingdanUpVo.setId(ywYudingHuiyiDingdanQueryVo.getId());
                ywYudingHuiyiDingdanUpVo.setZhuangtai(YwYudingHuiyiDingdanConstants.ZHUANGTAI_YIHUIFU);
                ywYudingHuiyiXuqiudanMapper.updateByPrimaryKeySelective(ywYudingHuiyiDingdanUpVo);
            }


            //3.保存订单数据信息
            //3.1保存会议预订单数据
            YwYudingHuiyiDingdan dingdan = new YwYudingHuiyiDingdan();
            BeanUtils.copyProperties(params, dingdan);
            dingdan.setHuiyuanId(-1);
            dingdan.setTianbaoshijian(new Date());
            dingdan.setYudingriqi(new Date());
            dingdan.setChuangjianshijian(new Date());
            dingdan.setGengxinshijian(new Date());
            dingdan.setDingdanhao(dingdanhao);
            dingdan.setJiudianId(jiudianId);
            dingdan.setShanchubiaoshi(BusiConstant.DELFLAG_0);
            dingdan.setZhuangtai(YwYudingHuiyiDingdanConstants.ZHUANGTAI_YITIJIAO);
            if (params.getYudingxuqiudanId() != null) {
                dingdan.setYudingxuqiudanId(params.getYudingxuqiudanId());
                dingdan.setYudingxuqiudanhao(StringUtil.isNotBlank(params.getYudingxuqiudanhao()) ? params.getYudingxuqiudanhao() : null);
            }
            int dingdanRet = ywYudingHuiyiDingdanMapper.insertSelective(dingdan);
            if (dingdanRet <= 0) {
                log.error("traceId[" + traceId + "]写入会议预订单失败");
                resultVO.setMessage("写入会议预订单失败");
                return resultVO;
            }

            //会议订单Id、开始时间、结束时间
            int dingdanId = dingdan.getId();

            //3.2保存会议预订单子项数据信息
            for (MeetingBookingInfoHuichangDetailDto meetingBookingInfoHuichangDetailDto : meetingBookingInfoHuichangDetailList) {
                int fangjianid = meetingBookingInfoHuichangDetailDto.getHuichangId();
                //根据会场id，获取会场信息
                YwHuichang ywHuichang = huichangMapper.selectByPrimaryKey(fangjianid);
                if (ywHuichang == null) {
                    log.error("traceId[" + traceId + "]会场[" + fangjianid + "]查询无记录");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("会场[" + meetingBookingInfoHuichangDetailDto.getHuichangbianhao() + "]查询无记录");
                    return resultVO;
                }
                //会场编号
                String huichangbianmhao = meetingBookingInfoHuichangDetailDto.getHuichangbianhao();

                //判断该会场是否被占用
                YwFangtairiqijilu fangtairiqijiluQuery = new YwFangtairiqijilu();
                fangtairiqijiluQuery.setFangjianId(meetingBookingInfoHuichangDetailDto.getHuichangId());
                fangtairiqijiluQuery.setFangjianleibie(YwFangtairiqijiluConstants.FANGJIANLEIBIE_HUIYISHI);
                fangtairiqijiluQuery.setKaishishijian(meetingBookingInfoHuichangDetailDto.getKaishishijian());
                fangtairiqijiluQuery.setJieshushijian(meetingBookingInfoHuichangDetailDto.getJieshushijian());
                List<YwFangtairiqijilu> fangtairiqijiluList = fangtairiqijiluMapper.selectBookListByEntity(fangtairiqijiluQuery);
                if (!CollectionUtils.isEmpty(fangtairiqijiluList)) {
                    log.error("traceId[" + traceId + "]会场[" + huichangbianmhao + "]已占用");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("会场[" + huichangbianmhao + "]已占用");
                    return resultVO;
                } else {
                    YwFangtairiqijilu fangtairiqijilu = new YwFangtairiqijilu();
                    fangtairiqijilu.setJiudianId(jiudianId);
                    fangtairiqijilu.setFangjianId(fangjianid);
                    fangtairiqijilu.setKaishishijian(meetingBookingInfoHuichangDetailDto.getKaishishijian());
                    fangtairiqijilu.setJieshushijian(meetingBookingInfoHuichangDetailDto.getJieshushijian());
                    fangtairiqijilu.setFangjianzhuangtai(YwFangtairiqijiluConstants.FANGJIANZHUANGTAI_SUOFANG);
                    fangtairiqijilu.setFangjianleibie(YwFangtairiqijiluConstants.FANGJIANLEIBIE_HUIYISHI);
                    fangtairiqijilu.setYewuyuanId(dingdanId);
                    fangtairiqijilu.setYewuleixing(YwFangtairiqijiluConstants.YEWULEIXING_QIANTAIYUDINGDAN);
                    fangtairiqijilu.setZhuangtai(YwFangtairiqijiluConstants.ZHUANGTAI_YOUXIAO);
                    fangtairiqijilu.setCaozuobiaoshi(YwFangtairiqijiluConstants.CAOZUOBIAOSHI_YUDINGSUOFANG);
                    fangtairiqijilu.setBeizhu("会议预订单号[" + dingdanhao + "]锁房");
                    fangtairiqijilu.setChuangjianshijian(new Date());
                    fangtairiqijilu.setGengxinshijian(new Date());
                    fangtairiqijilu.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
                    int fangtairiqijiluRet = fangtairiqijiluMapper.insertSelective(fangtairiqijilu);
                    if (fangtairiqijiluRet <= 0) {
                        log.error("traceId[" + traceId + "]写入房态日期记录失败");
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        resultVO.setMessage("写入房态日期记录失败");
                        return resultVO;
                    }

                    //写入预订单子表
                    YwYudingHuiyiDingdanZixiang yudingDingdanZixiang = new YwYudingHuiyiDingdanZixiang();
                    yudingDingdanZixiang.setJiudianId(jiudianId);
                    yudingDingdanZixiang.setDingdanId(dingdanId);
                    yudingDingdanZixiang.setDingdanhao(dingdanhao);
                    yudingDingdanZixiang.setHuichangId(fangjianid);
                    yudingDingdanZixiang.setHuichangbianhao(huichangbianmhao);
                    yudingDingdanZixiang.setHuichangleixingId(meetingBookingInfoHuichangDetailDto.getHuichangleixing());
                    yudingDingdanZixiang.setChangxingmingcheng(meetingBookingInfoHuichangDetailDto.getHuichangleixingmingcheng());
                    yudingDingdanZixiang.setYouhuijia(meetingBookingInfoHuichangDetailDto.getYouhuijia());
                    yudingDingdanZixiang.setMendianjia(meetingBookingInfoHuichangDetailDto.getMendianjia());
//                    yudingDingdanZixiang.setYudingxingming(personalBookingDTO.getYudingrenxingming());
//                    yudingDingdanZixiang.setLianxidianhua();
                    yudingDingdanZixiang.setRuzhushijian(meetingBookingInfoHuichangDetailDto.getKaishishijian());
                    yudingDingdanZixiang.setLikaishijian(meetingBookingInfoHuichangDetailDto.getJieshushijian());
                    yudingDingdanZixiang.setBeizhu("会议预订单号[" + dingdanhao + "]预订会场");
                    yudingDingdanZixiang.setChuangjianshijian(new Date());
                    yudingDingdanZixiang.setGengxinshijian(new Date());
                    yudingDingdanZixiang.setShanchubiaoshi(BusiConstant.DELFLAG_0);
                    int zixiangRet = ywYudingHuiyiDingdanZixiangMapper.insertSelective(yudingDingdanZixiang);
                    //如果写入失败，手动回滚事务
                    if (zixiangRet <= 0) {
                        log.error("traceId[" + traceId + "]写入会议预订单子表失败");
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        resultVO.setMessage("写入会议预订单子表失败");
                        return resultVO;
                    }
                }
            }

            //4.写入操作日志
            YewuyonghuCaozuoRizhi yewuyonghuCaozuoRizhi = new YewuyonghuCaozuoRizhi("会议预订", "预订", "订单号[" + dingdanhao + "]");
            operateLogService.writeOperateLog(yewuyonghuCaozuoRizhi);

            //5.组装返回信息
            PersonalBookingResultDTO personalBookingResultDTO = new PersonalBookingResultDTO();
            personalBookingResultDTO.setDingdanId(dingdanId);
            personalBookingResultDTO.setDingdanhao(dingdanhao);

            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("会议预订成功");
            resultVO.setData(personalBookingResultDTO);
        } catch (Exception e) {
            log.error("traceId[" + traceId + "]会议预订失败，系统异常", e);
            throw new BizException("会议预订失败，系统异常");
        } finally {
            commonRedisTemplate.delete(redisLockKey);
        }
        long endTime = System.currentTimeMillis();
        long totalTime = endTime - startTime;
        log.info("traceId[" + traceId + "] 返回参数，respResult={} takeTimes={}", JSONObject.toJSONString(resultVO), DateFormatUtils.format(new Date(totalTime), "mm分ss秒SSS毫秒"));
        log.info("会议预订下单接口 end---------");
        return resultVO;
    }


    public ResultVO<RowsDataVO<MeetingBookingInfoDto>> list(MeetingBookingInfoQueryDto params) {
        RowsDataVO<MeetingBookingInfoDto> dataVO = new RowsDataVO<>();
        ResultVO<RowsDataVO<MeetingBookingInfoDto>> resultVO = ResultVO.success();
        QueryConditions conditions = new QueryConditions();

//        conditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, YwYudingHuiyiDingdanConstants.SHANCHUBIAOSHI_BUSHANCHU);
//        conditions.addCondition("zhuangtai", EConditionOperator.EQULS, params.getZhuangtai());
//        conditions.addCondition("yudingrenxingming", EConditionOperator.LIKE, params.getYudingrenxingming());
//        conditions.addCondition("yudingriqi", EConditionOperator.GREATER, params.getReservationStartDate());
//        conditions.addCondition("yudingriqi", EConditionOperator.LESS, params.getReservationEndDate());
//        conditions.addCondition("huiyuanId", EConditionOperator.EQULS, reservationUserCache.getCurrentUserId());
//        conditions.addCondition("dingdanhao", EConditionOperator.EQULS, params.getDingdanhao());
//
//        MyExample example = new MyExample(YwYudingHuiyiXuqiudan.class, conditions, params.getOrderFields());
        Page<MeetingBookingInfoDto> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
//        List<YwYudingHuiyiXuqiudan> ywYudingHuiyiDingdanList = ywYudingHuiyiXuqiudanMapper.selectByExample(example);
//        List<MeetingInqueryListQueryResultDto> listQueryResultDtos = ywYudingHuiyiDingdanList.stream().map(d -> {
//            MeetingInqueryListQueryResultDto resultDto = new MeetingInqueryListQueryResultDto();
//            BeanUtils.copyProperties(d, resultDto);
//
//            //得到所属关系列表
//            List<YwHuiyidanjiudianguanxi> huiyidanjiudianguanxiList = huiyidanjiudianguanxiMapper.selectByExample(Example.builder(YwHuiyidanjiudianguanxi.class)
//                    .where(Sqls.custom()
//                            .andEqualTo("huiyidingdanId", d.getId())
//                            .andEqualTo("shanchubiaoshi", HuiyidanjiudianguanxiConstants.SHANCHUBIAOSHI_BUSHANCHU)
//                    ).build()
//            );
//            List<HotelInquiryResultDTO> hotelInquiryResultDTOList = huiyidanjiudianguanxiList.stream().map(g -> {
//                HotelInquiryResultDTO hotelInquiryResultDTO = new HotelInquiryResultDTO();
//                //得到酒店基本信息
//                HotelInfoDetailQueryResultDTO jiudianInfoById = reservationCache.getJiudianInfoById(g.getJiudianId());
//                if (jiudianInfoById != null) {
//                    BeanUtils.copyProperties(jiudianInfoById, hotelInquiryResultDTO);
//                    hotelInquiryResultDTO.setJiudianMingcheng(jiudianInfoById.getMingcheng());
//                    hotelInquiryResultDTO.setJiudianId(g.getJiudianId());
//                    hotelInquiryResultDTO.setId(g.getId());
//                    hotelInquiryResultDTO.setManzuxuqiu(g.getManzuxuqiu());
//                    hotelInquiryResultDTO.setCankaojia(g.getCankaojia());
//
//                }
//                //得到回应消息
//                BeanUtils.copyProperties(g, hotelInquiryResultDTO);
//                return hotelInquiryResultDTO;
//            }).collect(Collectors.toList());
//            resultDto.setHotelInquiryResultList(hotelInquiryResultDTOList);
//
//            return resultDto;
//        }).collect(Collectors.toList());


        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(null);
        resultVO.setData(dataVO);
        return resultVO;
    }


    /**
     * 根据用户酒店id和查询条件获取会议单列表
     *
     * @param params
     * @return
     */
    public ResultVO<RowsDataVO<MeetingInqueryPmsListQueryResultDto>> pmsList(MeetingInqueryPmsListQueryDto params) {
        RowsDataVO<MeetingInqueryPmsListQueryResultDto> dataVO = new RowsDataVO<>();
        ResultVO<RowsDataVO<MeetingInqueryPmsListQueryResultDto>> resultVO = ResultVO.success();
        params.setJiudianId(manageUserCache.getJiudianId());

        Page<MeetingInqueryListQueryResultDto> page = PageHelper.startPage(params.getPageNo(), params.getPageSize());
        List<MeetingInqueryPmsListQueryResultDto> ywYudingHuiyiDingdanList = huiyidanjiudianguanxiMapper.getHuiyidanjiudianguanxiList(params);
        dataVO.setPageNo(page.getPageNum());
        dataVO.setTotalRecords(page.getTotal());
        dataVO.setTotalPages(page.getPages());
        dataVO.setRows(ywYudingHuiyiDingdanList);
        resultVO.setData(dataVO);
        return resultVO;
    }


    public ResultVO<MeetingInqueryPmsDetailResultDto> pmsDetail(MeetingInqueryPmsDetailQueryDto params) {
        ResultVO<MeetingInqueryPmsDetailResultDto> resultVO = ResultVO.fail();
        params.setJiudianId(manageUserCache.getJiudianId());
        MeetingInqueryPmsDetailResultDto meetingInqueryPmsDetailResultDto = huiyidanjiudianguanxiMapper.getHuiyidanByParams(params);
        if (meetingInqueryPmsDetailResultDto != null) {
            resultVO = ResultVO.success();
            resultVO.setData(meetingInqueryPmsDetailResultDto);
        }
        return resultVO;
    }


    public ResultVO<MeetingInqueryDetailResultDto> detail(MeetingInqueryDetailQueryDto params) {
        ResultVO<MeetingInqueryDetailResultDto> resultVO = ResultVO.fail();
        MeetingInqueryDetailResultDto resultDto = new MeetingInqueryDetailResultDto();

        YwYudingHuiyiXuqiudan dingdan = ywYudingHuiyiXuqiudanMapper.selectByPrimaryKey(params.getId());
        BeanUtils.copyProperties(dingdan, resultDto);
        List<YwHuiyidanjiudianguanxi> huiyidanjiudianguanxiList = huiyidanjiudianguanxiMapper.selectByExample(Example.builder(YwHuiyidanjiudianguanxi.class)
                .where(Sqls.custom()
                        .andEqualTo("huiyidingdanId", dingdan.getId())
                        .andEqualTo("shanchubiaoshi", HuiyidanjiudianguanxiConstants.SHANCHUBIAOSHI_BUSHANCHU)
                ).build()
        );
        List<MeetingInquiryHotelResultDTO> meetingInquiryHotelResultDTOList = huiyidanjiudianguanxiList.stream().map(h -> {
            MeetingInquiryHotelResultDTO resultDTO = new MeetingInquiryHotelResultDTO();

            HotelInfoDetailQueryResultDTO jiudianInfoById = hotelInfoCacheExtractor.getJiudianInfoById(h.getJiudianId());
            if (jiudianInfoById != null) {
                BeanUtils.copyProperties(jiudianInfoById, resultDTO);
            }
            return resultDTO;
        }).collect(Collectors.toList());
        resultDto.setHotelIdList(meetingInquiryHotelResultDTOList);
        resultVO = ResultVO.success();
        resultVO.setData(resultDto);

        return resultVO;
    }


    @Transactional
    public ResultVO verify(MeetingVerifyQueryDTO params) {
        huiyidanjiudianguanxiMapper.rejectHotelInfo(params.getOrderId());
        huiyidanjiudianguanxiMapper.verifyHotelInfo(params.getInfoId());
        YwYudingHuiyiXuqiudan ywYudingHuiyiDingdan = new YwYudingHuiyiXuqiudan();
        ywYudingHuiyiDingdan.setId(params.getOrderId());
        ywYudingHuiyiDingdan.setZhuangtai(YwYudingHuiyiDingdanConstants.ZHUANGTAI_YIHUIFU);
        int i = ywYudingHuiyiXuqiudanMapper.updateByPrimaryKeySelective(ywYudingHuiyiDingdan);
        if (i == 0) {
            throw new ArgumentException("修改订单状态失败");
        }
        return ResultVO.success();
    }


    @Transactional
    public ResultVO audit(MeetingInqueryAuditQueryDTO params) {
        ResultVO<PersonalBookingResultDTO> resultVO = ResultVO.fail();
        YwHuiyidanjiudianguanxi ywHuiyidanjiudianguanxiQueryVo = huiyidanjiudianguanxiMapper.selectByPrimaryKey(params.getInfoId());
        if (ywHuiyidanjiudianguanxiQueryVo != null) {
            if (ywHuiyidanjiudianguanxiQueryVo.getZhuangtai().intValue() > 1) {
                resultVO.setMessage("该订单已进行审核处理");
                return resultVO;
            }
            //1.更新需求关系单状态
            YwHuiyidanjiudianguanxi huiyidanjiudianguanxi = new YwHuiyidanjiudianguanxi();
            huiyidanjiudianguanxi.setId(params.getInfoId());
            huiyidanjiudianguanxi.setZhuangtai(params.getShenhezhuangtai());
            huiyidanjiudianguanxi.setManzuxuqiu((byte) (params.getShenhezhuangtai() == 2 ? 1 : 0));
            huiyidanjiudianguanxiMapper.updateByPrimaryKeySelective(huiyidanjiudianguanxi);

            //2.更新需求单状态，由已提交1改为已回复2,如果查询不到不进行更新操作
            YwYudingHuiyiXuqiudan ywYudingHuiyiDingdan = new YwYudingHuiyiXuqiudan();
            ywYudingHuiyiDingdan.setId(ywHuiyidanjiudianguanxiQueryVo.getHuiyidingdanId());
            ywYudingHuiyiDingdan.setZhuangtai(YwYudingHuiyiDingdanConstants.ZHUANGTAI_YITIJIAO);
            YwYudingHuiyiXuqiudan ywYudingHuiyiDingdanQueryVo = ywYudingHuiyiXuqiudanMapper.selectOne(ywYudingHuiyiDingdan);
            if (ywYudingHuiyiDingdanQueryVo != null) {
                YwYudingHuiyiXuqiudan ywYudingHuiyiDingdanUpVo = new YwYudingHuiyiXuqiudan();
                ywYudingHuiyiDingdanUpVo.setId(ywYudingHuiyiDingdanQueryVo.getId());
                ywYudingHuiyiDingdanUpVo.setZhuangtai(YwYudingHuiyiDingdanConstants.ZHUANGTAI_YIHUIFU);
                ywYudingHuiyiXuqiudanMapper.updateByPrimaryKeySelective(ywYudingHuiyiDingdanUpVo);
            }
            return ResultVO.success();
        }
        return resultVO;
    }

    /**
     * 效验请求参数
     *
     * @param reqVo
     * @return
     */
    @SuppressWarnings("unchecked")
    private ResultVO checkRequest(MeetingBookingInfoDto reqVo) {
        ResultVO<PersonalBookingResultDTO> resultVO = ResultVO.fail();
        // 效验请求参数
        if (reqVo != null) {
//            // 伙伴编号
//            if (!ResponseComm.checkLength(reqVo.getPartnerCode(), 6)) {
//                return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                        transactionID,
//                        Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                        Constant.ResponseCode.BUSINESS.CODE_001001,
//                        Constant.ResponseCode.BUSINESS.DESC_001001);
//            } else {
//                if (getPartner(reqVo.getPartnerCode()) == null) {
//                    return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                            transactionID,
//                            Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                            Constant.ResponseCode.BUSINESS.CODE_001001,
//                            Constant.ResponseCode.BUSINESS.DESC_001001);
//                }
//            }
//            // 伙伴订单号
//            if (!ResponseComm.checkLength(reqVo.getPartnerOrderNo(), 50)) {
//                return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                        transactionID,
//                        Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                        Constant.ResponseCode.BUSINESS.CODE_001030,
//                        Constant.ResponseCode.BUSINESS.DESC_001030);
//            }
//            // 客户姓名
//            if (!ResponseComm.checkLength(reqVo.getCustName(), 20)) {
//                return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                        transactionID,
//                        Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                        Constant.ResponseCode.BUSINESS.CODE_002250,
//                        "客户姓名格式不正确");
//            }
            // 证件类型 暂只支持身份证
            if (!(1 == reqVo.getZhengjianleixing() || 2 == reqVo.getZhengjianleixing())) {
                resultVO.setMessage("证件类型不正确");
                return resultVO;
            }
            // 证件号码
            if (!CommUtil.checkCertainLength(reqVo.getZhengjianhao(), 5, 18)) {
                resultVO.setMessage("证件号码格式不正确");
                return resultVO;
            }
//
//            // 抹零金额 若无，默认0.00
//            // 手续费 若无，默认0.00
//            // 外币折美 -
//            // 人民币 -
//            // 自取日期
//            if (!ResponseComm.checkDate(reqVo.getReceiverDate(), "yyyy-MM-dd")) {
//                return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                        transactionID,
//                        Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                        Constant.ResponseCode.BUSINESS.CODE_002250,
//                        "自取日期格式不正确");
//            }
//
//            // 订单状态 -
//            // 下单完成时间 -
//            // 支付完成时间-
//            // 优惠劵代码 -
//            reqVo.setCouponCode(StringUtil.isBlank(reqVo.getCouponCode()) ? null : reqVo.getCouponCode());
//            // 币种列表
//            List<CurrencyVo> list = reqVo.getCurrencyList();
//            for (CurrencyVo curr : list) {
//                // CURRENCY 3
//                if (!ResponseComm.checkCertainLength(curr.getCurrency(), 3) || getCurreny(curr.getCurrency()) == null) {
//                    return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                            transactionID,
//                            Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                            Constant.ResponseCode.BUSINESS.CODE_002201,
//                            Constant.ResponseCode.BUSINESS.DESC_002201);
//                }
//                // AMOUNT 15
//                if (!ResponseComm.checkLength(curr.getAmount(), 15) || !NumberUtil.isNumber(curr.getAmount())) {
//                    return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                            transactionID,
//                            Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                            Constant.ResponseCode.BUSINESS.CODE_002250,
//                            "外币金额格式不正确");
//                }
//                // RATE 16,4
//                if (!ResponseComm.checkLength(curr.getRate(), 21) || !NumberUtil.isNumber(curr.getRate(), 16, 4)) {
//                    return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                            transactionID,
//                            Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                            Constant.ResponseCode.BUSINESS.CODE_002250,
//                            "成交汇率格式不正确");
//                }
//                // RMB_AMOUT 11
//                if (!ResponseComm.checkLength(reqVo.getRmbAmount(), 11) || !NumberUtil.isNumber(reqVo.getRmbAmount())) {
//                    return (Resp<OrderConfirmRspVo>) ResponseComm.generateErrorResp(
//                            transactionID,
//                            Constant.ResponseCode.BUSINESS.TYPE_CODE,
//                            Constant.ResponseCode.BUSINESS.CODE_002250,
//                            "应付人民币格式不正确");
//                }
            // SETTLE_RATE -
            // TODOLLAR_AMOUNT -
            //}
            //1.校验请求参数
            if (DateCalendarUtil.getDiffDays(reqVo.getKaishishijian(), reqVo.getJieshushijian()) < 0) {
                resultVO.setMessage("会议报到时间、离店时间，输入数据不正确");
                return resultVO;
            }
            //2.校验会议预订数据正确性
            String allowTime = SysCodeUtil.getSysParamDesc("yd.huiyikaishishijianchazhi".toUpperCase(), "allowtime");
            int allowTimeHour = 0;
            if (StringUtil.isNotBlank(allowTime)) {
                allowTimeHour = Integer.parseInt(allowTime);
            } else {
                allowTimeHour = BusiConstant.ALLOWTIME;
            }
            List<MeetingBookingInfoHuichangDetailDto> meetingBookingInfoHuichangDetailList = reqVo.getDetailList();
            for (MeetingBookingInfoHuichangDetailDto meetingBookingInfoHuichangDetailDto : meetingBookingInfoHuichangDetailList) {
                //2.1校验会议开始时间
                Date kaishishijian = meetingBookingInfoHuichangDetailDto.getKaishishijian();
                Date allowDate = DateTool.parse(DateTool.getDateTime("yyyy-MM-dd HH:mm:00"), "yyyy-MM-dd HH:mm:ss");
                allowDate = DateTool.addDaysByField(allowDate, allowTimeHour, Calendar.HOUR);
                if (!kaishishijian.after(allowDate)) {
                    resultVO.setMessage("会议预订开始时间选择不对，请重新输入");
                    return resultVO;
                }
                if (!kaishishijian.before(meetingBookingInfoHuichangDetailDto.getJieshushijian())) {
                    resultVO.setMessage("会议预订开始时间早于会议结束时间，请重新输入");
                    return resultVO;
                }
            }
            return ResultVO.success();
        } else {
            return ResultVO.fail("请求参数不正确");
        }
    }

    public ResultVO<RowsDataVO<MeetingTeamListResultDTO>> meetingTeamList(MeetingTeamListDTO params) {
        ResultVO resultVO = ResultVO.success();
        String order = params.getOrderFields().stream().filter(o -> !StringUtils.isEmpty(o.getOrderField())).map(o -> o.getOrderField() + (o.getOrderDesc() ? " DESC" : " ASC")).reduce((s1, s2) -> s1 + "," + s2).orElse("");
        Page<MeetingTeamListResultDTO> page = PageHelper.startPage(params.getPageNo(),params.getPageSize(),order);
        //List<MeetingTeamListResultDTO> teamList = ywYudingHuiyiDingdanMapper.selectMeetingTeamList(params);
        RowsDataVO<MeetingTeamListResultDTO> rowsDataVO = new RowsDataVO<>();

        rowsDataVO.setPageNo(page.getPageNum());
        rowsDataVO.setTotalRecords(page.getTotal());
        rowsDataVO.setTotalPages(page.getPages());
        //rowsDataVO.setRows(teamList);
        resultVO.setData(rowsDataVO);
        return resultVO;
    }
}
